﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Compilex.Syntatic.Declarations
{
    public class WhileBlock
    {
        public SyntaticContext Context { get; set; }
        public WhileBlock(SyntaticContext context)
        {
            this.Context = context;
        }

        // antes da expressao
        public void StartBeforeExpression()
        {
            Context.CommandBlockStack.Current.CodeGenerator.StartWhile();
            Context.ScopeManager.OpenScope();
        }

        // depois da expressão
        public void AfterExpression()
        {
            BooleanExpression condition = Context.ExpressionStack.JustPopped;
            if (condition.Type.TypeValue != TypeValue.boolex)
            {
                throw new SyntaticException("Expressões '" + condition.Type + "' não podem ser usadas como condição.");
            }

            Context.CommandBlockStack.Current.CodeGenerator.AfterWhileExpression();
        }

        // finex
        public void End()
        {
            Context.CommandBlockStack.Current.CodeGenerator.EndWhile();
            Context.ScopeManager.CloseScope();
        }
    }
}
